package code.rocky.sync;

import java.util.concurrent.TimeUnit;

public class SyncTest8 implements Runnable {

    private static int INIT_VALUE = 0;
    private static int TOTAL = 10;


    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        if ("t1".equals(name)) {
            one();
        }
        else {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            two();
        }
    }

    public synchronized static boolean one() { // 方法上是 this 锁
        try {
            System.out.println(Thread.currentThread().getName() + "=====one start============");
            TimeUnit.SECONDS.sleep(2);
            System.out.println(Thread.currentThread().getName() + "=====one end============");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " INIT_VALUE: " + (INIT_VALUE++));
        return false;
    }

    public synchronized static boolean two() { // 这个时候 锁 class
        System.out.println(Thread.currentThread().getName() + " INIT_VALUE: " + (INIT_VALUE++));
        return false;
    }

    public static void main(String[] args) {
        SyncTest8 test5 = new SyncTest8();
        SyncTest8 test7 = new SyncTest8();
        new Thread(test5, "t1").start();
        new Thread(test7, "t2").start();
        new Thread(test7, "t3").start();

    }


}


